package org.opens.mybatisplus.controller;

import org.opens.mybatisplus.pojo.JacksonIgnoreDO;
import org.opens.mybatisplus.pojo.dto.JackonDTO;
import org.opens.mybatisplus.pojo.result.ResultBean;
import org.opens.mybatisplus.service.JacksonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.List;

@RestController
@RequestMapping(value = "/jackson")
public class JackonController {

    @Autowired
    private JacksonService jacksonService;

    /**
     * 简介:
     *      -请求方式
     *          http://localhost/jackon/format      post请求且Content-Type=application/json
     *      -携带数据
     *          {
     *              "id" : 1,
     *              "name" : "zs",
     *              "createTime" : "2019-09-01 12:25:55"
     *          }
     *      -返回值
     *          2019-09-01
     *      -总结
     *          说明接收成功.@JsonFormat注解起到作用.
     * @param jackonDTO
     * @return
     */
    @PostMapping(value = "/format")
    public String jsonFormatTest(
            @RequestBody JackonDTO jackonDTO
    ) {
        return new SimpleDateFormat("yyyy-MM-dd").format(jackonDTO.getCreateTime());
    }

    /**
     * 功能:
     *      -请求方式
     *          http://localhost/jackson/ignore
     *      -返回数据
     *          {
     *              "code": 200,
     *              "message": "请求成功",
     *              "data": [
     *              {
     *                  "id": 1,
     *                  "name": "zz"
     *              },
     *              {
     *                  "id": 2,
     *                  "name": "cc"
     *              },
     *              ...
     *          }
     *      -总结
     *          可以发现, 这个DO中的@JsonIgnore注解成功使最终结果忽略了score字段.
     * @return
     */
    @GetMapping(value = "/ignore")
    public ResultBean<List<JacksonIgnoreDO>> getDate() {
        return ResultBean.success(jacksonService.listOfAll());
    }

}
